﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface IYFKZDDAL : IDAL<CW_YFKZD>
    {
        List<YFKZDModel> GetYFKZD(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
        List<CGDModel> GetCGDNotInYFKZD(string orderExpr, int? pageIndex, int pageSize, out int totalCount);
    }
    public class CW_YFKZDDAL:BaseDAL<CW_YFKZD>,IYFKZDDAL
    {
        public CW_YFKZDDAL(IUnitOfWork context)
            : base(context)
        { }
        public List<YFKZDModel> GetYFKZD(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from yfkzd in dataContext.CW_YFKZD
                         join cgd in dataContext.CGD on yfkzd.CGDID equals cgd.ID
                         join gys in dataContext.ZL_GYS on cgd.GYSID equals gys.ID
                         join users in dataContext.Users on yfkzd.CZRYID equals users.Id
                         join jsfs in dataContext.CS_JSFS on yfkzd.JSFSBH equals jsfs.BH
                         select new YFKZDModel
                         {
                             ID = yfkzd.ID,
                             BH = yfkzd.BH,
                             CGDID = yfkzd.CGDID,
                             CGDBH = cgd.BH,
                             GYSID = cgd.GYSID,
                             GYSMC = gys.MC,
                             GYSJC = gys.JIANCHENG,
                             CZRYID = yfkzd.CZRYID,
                             CZRYName = users.Name,
                             CJSJ = yfkzd.CJSJ,
                             DZSJ = yfkzd.DZSJ,
                             JQSJ = yfkzd.JQSJ,
                             YFZE = yfkzd.YFZE,
                             YIFUZE = yfkzd.YIFUZE,
                             ZDZT = yfkzd.ZDZT,
                             JSFSBH = yfkzd.JSFSBH,
                             JSFSMC = jsfs.MC
                         };
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("ID") && !string.IsNullOrEmpty(dc["ID"]))
                {
                    Guid temp = Guid.Parse(dc["ID"]);
                    result = result.Where(t => t.ID == temp);
                }
                if (dc.ContainsKey("BH") && !string.IsNullOrEmpty(dc["BH"]))
                {
                    string temp = dc["BH"];
                    result = result.Where(t => t.BH == temp);
                }
                if (dc.ContainsKey("ZDZT") && !string.IsNullOrEmpty(dc["ZDZT"]))
                {
                    int temp = Convert.ToInt32(dc["ZDZT"]);
                    result = result.Where(t => t.ZDZT == temp);
                }
                if (dc.ContainsKey("CGDBH") && !string.IsNullOrEmpty(dc["CGDBH"]))
                {
                    string temp = dc["CGDBH"];
                    result = result.Where(t => t.CGDBH == temp);
                }
                if (dc.ContainsKey("GYSJC") && !string.IsNullOrEmpty(dc["GYSJC"]))
                {
                    string temp = dc["GYSJC"];
                    result = result.Where(t => t.GYSJC == temp);
                }
                if (dc.ContainsKey("CZRYName") && !string.IsNullOrEmpty(dc["CZRYName"]))
                {
                    string temp = dc["CZRYName"];
                    result = result.Where(t => t.CZRYName == temp);
                }
                if (dc.ContainsKey("JSFSBH") && !string.IsNullOrEmpty(dc["JSFSBH"]))
                {
                    string temp = dc["JSFSBH"];
                    result = result.Where(t => t.JSFSBH == temp);
                }

                if (dc.ContainsKey("GYSID") && !string.IsNullOrEmpty(dc["GYSID"]))
                {
                    Guid GYSID = Guid.Parse(dc["GYSID"]);
                    List<Guid> ids = dataContext.CGD.Where(t => t.GYSID == GYSID).Select(t => t.ID).ToList();
                    result = result.Where(t => ids.Contains(t.GYSID));
                }

                if (dc.Keys.Contains("CJSJ") && !string.IsNullOrEmpty(dc["CJSJ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["CJSJ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.CJSJ == keyVal);
                }
                if (dc.Keys.Contains("DZSJ") && !string.IsNullOrEmpty(dc["DZSJ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["DZSJ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.DZSJ == keyVal);
                }
                if (dc.Keys.Contains("JQSJ") && !string.IsNullOrEmpty(dc["JQSJ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["JQSJ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.JQSJ == keyVal);
                }

                if (dc.ContainsKey("YFZEMin") && !string.IsNullOrEmpty(dc["YFZEMin"]))
                {
                    double temp = double.Parse(dc["YFZEMin"]);
                    result = result.Where(t => t.YFZE >= temp);
                }
                if (dc.ContainsKey("YFZEMax") && !string.IsNullOrEmpty(dc["YFZEMax"]))
                {
                    double temp = double.Parse(dc["YFZEMax"]);
                    result = result.Where(t => t.YFZE <= temp);
                }
            }
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();

        }
        //
        public List<CGDModel> GetCGDNotInYFKZD(string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from cgd in dataContext.CGD
                         join bm in dataContext.Departments on cgd.BMID equals bm.Id
                         join ry in dataContext.Users on cgd.RYID equals ry.Id
                         join gys in dataContext.ZL_GYS on cgd.GYSID equals gys.ID
                         join czry in dataContext.Users on cgd.CZRYID equals czry.Id
                         where !dataContext.CW_YFKZD.Select(t => t.CGDID).Contains(cgd.ID)
                         select new CGDModel
                         {
                             BH = cgd.BH,
                             ID = cgd.ID,
                             BMID = cgd.BMID,
                             BMMC = bm.Name,
                             RYID = cgd.RYID,
                             RYName = ry.Name,
                             GYSID = cgd.GYSID,
                             GYSMC = gys.MC,
                             GYSJC = gys.JIANCHENG,
                             ZT = cgd.ZT,
                             RQ = cgd.RQ,
                             CJSJ = cgd.CJSJ,
                             DHSJ = cgd.DHSJ,
                             CZRYID = cgd.CZRYID,
                             CZRYName = czry.Name
                         };
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }

    }
}
